//=====================================================================
//    COPYRIGHT(C) Innobeam
//    ALL RIGHTS RESERVED
//=====================================================================
//Filename    : unpackRGS.v rev 1.0
//Created On  : 2019-10-11
//Author      : fei.yu
//Description :	unpack the EtherCAT RX words from lan9252 (RGS)
//Include     : 
//Modification: fei.yu
//=====================================================================
module unpack_BFH(	iClk 			,
					iRst_n			,
					//to lan9252 module
					ivData_pram 	,		//32bit input PRAM data;
					ivDpramAddrRx 	,		//4bit input PRAM address
					iWenDpram		,		//1bit input PRAM data write enable(valid)
					//from main_logic
					iWriteCtrl		,		//1bit from main_logic,high 
					//to data_operation & pack module
					ovBSC_LED_1,		//16bit output return	
					ovBSC_LED_2,	//16bit output return	
					ovHHC_Gantry_1,
					ovHHC_Collimator_1,		//16bit output return	
					ovHHC_Swing_1,			//16bit output return		
					ovHHC_MLCX1_1,	//16bit output return			
					ovHHC_MLCX2_1,	//16bit output return			
					ovHHC_JawY1_1,	//16bit output return				
					ovHHC_JawY2_1,		//16bit output return		
					ovHHC_CouchVRT_1,		//16bit output return	
					ovHHC_CouchLAT_1,	//16bit output return
					ovHHC_CouchLNG_1,
					ovHHC_CouchLED1_1,
					ovHHC_CouchLED2_1,
					ovHHC_DisplayState_1,
					ovHHC_CouchLNGPLAN_1,
					ovHHC_MLCX_1,
					ovHHC_MLCY_1,
					ovHHC_Gantry_2,
					ovHHC_Collimator_2,		//16bit output return	
					ovHHC_Swing_2,			//16bit output return		
					ovHHC_MLCX1_2,	//16bit output return			
					ovHHC_MLCX2_2,	//16bit output return			
					ovHHC_JawY1_2,	//16bit output return				
					ovHHC_JawY2_2,		//16bit output return		
					ovHHC_CouchVRT_2,		//16bit output return	
					ovHHC_CouchLAT_2,	//16bit output return
					ovHHC_CouchLNG_2,
					ovHHC_CouchLED1_2,
					ovHHC_CouchLED2_2,
					ovHHC_DisplayState_2,
					ovHHC_CouchLNGPLAN_2,
					ovHHC_MLCX_2,
					ovHHC_MLCY_2
					);
//========================================================================
//    parameter
//========================================================================

parameter	DLY_TIME = 32'd200_000;

//========================END=============================================


//========================================================================
//    port
//========================================================================
input 					iClk;
input 					iRst_n;

input		[31:0]		ivData_pram;
input		[9:0]		ivDpramAddrRx;
input					iWenDpram;
input					iWriteCtrl;

output		[15:0]		ovBSC_LED_1;		
output		[15:0]		ovBSC_LED_2;	
output		[15:0]		ovHHC_Gantry_1;
output		[15:0]		ovHHC_Collimator_1;		
output		[15:0]		ovHHC_Swing_1;		
output		[15:0]		ovHHC_MLCX1_1;		
output		[15:0]		ovHHC_MLCX2_1;		
output		[15:0]		ovHHC_JawY1_1;		
output		[15:0]		ovHHC_JawY2_1;	
output		[15:0]		ovHHC_CouchVRT_1;
output		[15:0]		ovHHC_CouchLAT_1;	
output		[15:0]		ovHHC_CouchLNG_1;
output		[15:0]		ovHHC_CouchLED1_1;
output		[15:0]		ovHHC_CouchLED2_1;
output		[15:0]		ovHHC_DisplayState_1;
output		[15:0]		ovHHC_CouchLNGPLAN_1;
output		[15:0]		ovHHC_MLCX_1;	
output		[15:0]		ovHHC_MLCY_1;	
output		[15:0]		ovHHC_Gantry_2;
output		[15:0]		ovHHC_Collimator_2;		
output		[15:0]		ovHHC_Swing_2;		
output		[15:0]		ovHHC_MLCX1_2;		
output		[15:0]		ovHHC_MLCX2_2;		
output		[15:0]		ovHHC_JawY1_2;		
output		[15:0]		ovHHC_JawY2_2;	
output		[15:0]		ovHHC_CouchVRT_2;
output		[15:0]		ovHHC_CouchLAT_2;	
output		[15:0]		ovHHC_CouchLNG_2;
output		[15:0]		ovHHC_CouchLED1_2;
output		[15:0]		ovHHC_CouchLED2_2;
output		[15:0]		ovHHC_DisplayState_2;
output		[15:0]		ovHHC_CouchLNGPLAN_2;
output		[15:0]		ovHHC_MLCX_2;	
output		[15:0]		ovHHC_MLCY_2;

//========================END=============================================


//========================================================================
//*Unpack
//========================================================================
reg		[15:0]		rvBSC_LED_1;		
reg		[15:0]		rvBSC_LED_2;	
reg		[15:0]		rvHHC_Gantry_1;
reg		[15:0]		rvHHC_Collimator_1;		
reg		[15:0]		rvHHC_Swing_1;		
reg		[15:0]		rvHHC_MLCX1_1;		
reg		[15:0]		rvHHC_MLCX2_1;		
reg		[15:0]		rvHHC_JawY1_1;		
reg		[15:0]		rvHHC_JawY2_1;	
reg		[15:0]		rvHHC_CouchVRT_1;
reg		[15:0]		rvHHC_CouchLAT_1;	
reg		[15:0]		rvHHC_CouchLNG_1;
reg		[15:0]		rvHHC_CouchLED1_1;
reg		[15:0]		rvHHC_CouchLED2_1;
reg		[15:0]		rvHHC_DisplayState_1;
reg		[15:0]		rvHHC_CouchLNGPLAN_1;
reg		[15:0]		rvHHC_MLCX_1;	
reg		[15:0]		rvHHC_MLCY_1;	
reg		[15:0]		rvHHC_Gantry_2;
reg		[15:0]		rvHHC_Collimator_2;		
reg		[15:0]		rvHHC_Swing_2;		
reg		[15:0]		rvHHC_MLCX1_2;		
reg		[15:0]		rvHHC_MLCX2_2;		
reg		[15:0]		rvHHC_JawY1_2;		
reg		[15:0]		rvHHC_JawY2_2;	
reg		[15:0]		rvHHC_CouchVRT_2;
reg		[15:0]		rvHHC_CouchLAT_2;	
reg		[15:0]		rvHHC_CouchLNG_2;
reg		[15:0]		rvHHC_CouchLED1_2;
reg		[15:0]		rvHHC_CouchLED2_2;
reg		[15:0]		rvHHC_DisplayState_2;
reg		[15:0]		rvHHC_CouchLNGPLAN_2;
reg		[15:0]		rvHHC_MLCX_2;	
reg		[15:0]		rvHHC_MLCY_2;
reg		rHold;
reg		[31:0]      rvCnt;

assign     ovBSC_LED_1 				= rvBSC_LED_1;	
assign     ovBSC_LED_2 				= rvBSC_LED_2;	
assign     ovHHC_Gantry_1 			= rvHHC_Gantry_1;
assign     ovHHC_Collimator_1 		= rvHHC_Collimator_1;	
assign     ovHHC_Swing_1 			= rvHHC_Swing_1;		
assign     ovHHC_MLCX1_1 			= rvHHC_MLCX1_1;		
assign     ovHHC_MLCX2_1 			= rvHHC_MLCX2_1;		
assign     ovHHC_JawY1_1 			= rvHHC_JawY1_1;		
assign     ovHHC_JawY2_1 			= rvHHC_JawY2_1;
assign     ovHHC_CouchVRT_1 		= rvHHC_CouchVRT_1;	 		
assign     ovHHC_CouchLAT_1 		= rvHHC_CouchLAT_1;	
assign     ovHHC_CouchLNG_1 		= rvHHC_CouchLNG_1;			
assign     ovHHC_CouchLED1_1 		= rvHHC_CouchLED1_1;
assign     ovHHC_CouchLED2_1		= rvHHC_CouchLED2_1;
assign     ovHHC_DisplayState_1 	= rvHHC_DisplayState_1;
assign     ovHHC_CouchLNGPLAN_1 	= rvHHC_CouchLNGPLAN_1;
assign     ovHHC_MLCX_1 			= rvHHC_MLCX_1;	
assign     ovHHC_MLCY_1 			= rvHHC_MLCY_1;	
assign     ovHHC_Gantry_2 			= rvHHC_Gantry_2;
assign     ovHHC_Collimator_2 		= rvHHC_Collimator_2;	
assign     ovHHC_Swing_2 			= rvHHC_Swing_2;		
assign     ovHHC_MLCX1_2 			= rvHHC_MLCX1_2;		
assign     ovHHC_MLCX2_2 			= rvHHC_MLCX2_2;		
assign     ovHHC_JawY1_2 			= rvHHC_JawY1_2;		
assign     ovHHC_JawY2_2 			= rvHHC_JawY2_2;
assign     ovHHC_CouchVRT_2 		= rvHHC_CouchVRT_2;	 		
assign     ovHHC_CouchLAT_2 		= rvHHC_CouchLAT_2;	
assign     ovHHC_CouchLNG_2 		= rvHHC_CouchLNG_2;			
assign     ovHHC_CouchLED1_2 		= rvHHC_CouchLED1_2;
assign     ovHHC_CouchLED2_2		= rvHHC_CouchLED2_2;
assign     ovHHC_DisplayState_2 	= rvHHC_DisplayState_2;
assign     ovHHC_CouchLNGPLAN_2 	= rvHHC_CouchLNGPLAN_2;
assign     ovHHC_MLCX_2 			= rvHHC_MLCX_2;	
assign     ovHHC_MLCY_2 			= rvHHC_MLCY_2;




//receive data from PRAM(address decode)	
always @(posedge iClk or negedge iRst_n)begin
	if(iRst_n==1'b0)begin		
		rvBSC_LED_1               <= 16'd0;		
		rvBSC_LED_2               <= 16'd0;	
		rvHHC_Gantry_1            <= 16'd0;
		rvHHC_Collimator_1        <= 16'd0;		
		rvHHC_Swing_1             <= 16'd0;		
		rvHHC_MLCX1_1             <= 16'd0;		
		rvHHC_MLCX2_1             <= 16'd0;		
		rvHHC_JawY1_1             <= 16'd0;		
		rvHHC_JawY2_1             <= 16'd0;	
		rvHHC_CouchVRT_1          <= 16'd0;
		rvHHC_CouchLAT_1          <= 16'd0;	
		rvHHC_CouchLNG_1          <= 16'd0;
		rvHHC_CouchLED1_1         <= 16'd0;
		rvHHC_CouchLED2_1         <= 16'd0;
		rvHHC_DisplayState_1      <= 16'd0;
		rvHHC_CouchLNGPLAN_1      <= 16'd0;
		rvHHC_MLCX_1              <= 16'd0;	
		rvHHC_MLCY_1              <= 16'd0;	
		rvHHC_Gantry_2            <= 16'd0;
		rvHHC_Collimator_2        <= 16'd0;		
		rvHHC_Swing_2             <= 16'd0;		
		rvHHC_MLCX1_2             <= 16'd0;		
		rvHHC_MLCX2_2             <= 16'd0;		
		rvHHC_JawY1_2             <= 16'd0;		
		rvHHC_JawY2_2             <= 16'd0;	
		rvHHC_CouchVRT_2          <= 16'd0;
		rvHHC_CouchLAT_2          <= 16'd0;	
		rvHHC_CouchLNG_2          <= 16'd0;
		rvHHC_CouchLED1_2         <= 16'd0;
		rvHHC_CouchLED2_2         <= 16'd0;
		rvHHC_DisplayState_2      <= 16'd0;
		rvHHC_CouchLNGPLAN_2      <= 16'd0;
		rvHHC_MLCX_2              <= 16'd0;	
		rvHHC_MLCY_2              <= 16'd0;
		end
	else begin
		if((rHold == 1'b0) &&(iWriteCtrl== 1'b1))begin
			case(ivDpramAddrRx)
			10'd0:begin
				rvBSC_LED_1<= iWenDpram?ivData_pram[15:0]:rvBSC_LED_1;
				rvBSC_LED_2<= iWenDpram?ivData_pram[31:16]:rvBSC_LED_2;
				end
			10'd1:begin
				rvHHC_Gantry_1<= iWenDpram?ivData_pram[15:0]:rvHHC_Gantry_1;
				rvHHC_Collimator_1<= iWenDpram?ivData_pram[31:16]:rvHHC_Collimator_1;
				end	
			10'd2:begin
				rvHHC_Swing_1<= iWenDpram?ivData_pram[15:0]:rvHHC_Swing_1;
				rvHHC_MLCX1_1<= iWenDpram?ivData_pram[31:16]:rvHHC_MLCX1_1;
				end
			10'd3:begin
				rvHHC_MLCX2_1<= iWenDpram?ivData_pram[15:0]:rvHHC_MLCX2_1;
				rvHHC_JawY1_1<= iWenDpram?ivData_pram[31:16]:rvHHC_JawY1_1;
				end
			10'd4:begin
				rvHHC_JawY2_1<= iWenDpram?ivData_pram[15:0]:rvHHC_JawY2_1;
				rvHHC_CouchVRT_1<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchVRT_1;
				end	
			10'd5:begin
				rvHHC_CouchLAT_1<= iWenDpram?ivData_pram[15:0]:rvHHC_CouchLAT_1;
				rvHHC_CouchLNG_1<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLNG_1;
				end
			10'd6:begin
				rvHHC_CouchLED1_1<= iWenDpram?ivData_pram[15:0]:rvHHC_CouchLED1_1;
				rvHHC_CouchLED2_1<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLED2_1;
				end
			10'd7:begin
				rvHHC_DisplayState_1<= iWenDpram?ivData_pram[15:0]:rvHHC_DisplayState_1;
				rvHHC_CouchLNGPLAN_1<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLNGPLAN_1;
				end	
			10'd8:begin
				rvHHC_MLCX_1<= iWenDpram?ivData_pram[15:0]:rvHHC_MLCX_1;
				rvHHC_MLCY_1<= iWenDpram?ivData_pram[31:16]:rvHHC_MLCY_1;
				end
			10'd9:begin
				rvHHC_Gantry_2<= iWenDpram?ivData_pram[15:0]:rvHHC_Gantry_2;
				rvHHC_Collimator_2<= iWenDpram?ivData_pram[31:16]:rvHHC_Collimator_2;
				end	
			10'd10:begin
				rvHHC_Swing_2<= iWenDpram?ivData_pram[15:0]:rvHHC_Swing_2;
				rvHHC_MLCX1_2<= iWenDpram?ivData_pram[31:16]:rvHHC_MLCX1_2;
				end
			10'd11:begin
				rvHHC_MLCX2_2<= iWenDpram?ivData_pram[15:0]:rvHHC_MLCX2_2;
				rvHHC_JawY1_2<= iWenDpram?ivData_pram[31:16]:rvHHC_JawY1_2;
				end
			10'd12:begin
				rvHHC_JawY2_2<= iWenDpram?ivData_pram[15:0]:rvHHC_JawY2_2;
				rvHHC_CouchVRT_2<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchVRT_2;
				end	
			10'd13:begin
				rvHHC_CouchLAT_2<= iWenDpram?ivData_pram[15:0]:rvHHC_CouchLAT_2;
				rvHHC_CouchLNG_2<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLNG_2;
				end
			10'd14:begin
				rvHHC_CouchLED1_2<= iWenDpram?ivData_pram[15:0]:rvHHC_CouchLED1_2;
				rvHHC_CouchLED2_2<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLED2_2;
				end
			10'd15:begin
				rvHHC_DisplayState_2<= iWenDpram?ivData_pram[15:0]:rvHHC_DisplayState_2;
				rvHHC_CouchLNGPLAN_2<= iWenDpram?ivData_pram[31:16]:rvHHC_CouchLNGPLAN_2;
				end	
			10'd16:begin
				rvHHC_MLCX_2<= iWenDpram?ivData_pram[15:0]:rvHHC_MLCX_2;
				rvHHC_MLCY_2<= iWenDpram?ivData_pram[31:16]:rvHHC_MLCY_2;
				end
			default:begin		
				rvBSC_LED_1               <=rvBSC_LED_1;		
				rvBSC_LED_2               <=rvBSC_LED_2;	
				rvHHC_Gantry_1            <= rvHHC_Gantry_1;
				rvHHC_Collimator_1        <= rvHHC_Collimator_1;		
				rvHHC_Swing_1             <= rvHHC_Swing_1;		
				rvHHC_MLCX1_1             <= rvHHC_MLCX1_1;		
				rvHHC_MLCX2_1             <= rvHHC_MLCX2_1;		
				rvHHC_JawY1_1             <= rvHHC_JawY1_1;		
				rvHHC_JawY2_1             <= rvHHC_JawY2_1;	
				rvHHC_CouchVRT_1          <= rvHHC_CouchVRT_1;
				rvHHC_CouchLAT_1          <= rvHHC_CouchLAT_1;	
				rvHHC_CouchLNG_1          <= rvHHC_CouchLNG_1;
				rvHHC_CouchLED1_1         <= rvHHC_CouchLED1_1;
				rvHHC_CouchLED2_1         <= rvHHC_CouchLED2_1;
				rvHHC_DisplayState_1      <= rvHHC_DisplayState_1;
				rvHHC_CouchLNGPLAN_1      <= rvHHC_CouchLNGPLAN_1;
				rvHHC_MLCX_1              <= rvHHC_MLCX_1;	
				rvHHC_MLCY_1              <= rvHHC_MLCY_1;	
				rvHHC_Gantry_2            <= rvHHC_Gantry_2;
				rvHHC_Collimator_2        <= rvHHC_Collimator_2;		
				rvHHC_Swing_2             <= rvHHC_Swing_2;		
				rvHHC_MLCX1_2             <= rvHHC_MLCX1_2;		
				rvHHC_MLCX2_2             <= rvHHC_MLCX2_2;		
				rvHHC_JawY1_2             <= rvHHC_JawY1_2;		
				rvHHC_JawY2_2             <= rvHHC_JawY2_2;	
				rvHHC_CouchVRT_2          <= rvHHC_CouchVRT_2;
				rvHHC_CouchLAT_2          <= rvHHC_CouchLAT_2;	
				rvHHC_CouchLNG_2          <= rvHHC_CouchLNG_2;
				rvHHC_CouchLED1_2         <= rvHHC_CouchLED1_2;
				rvHHC_CouchLED2_2         <= rvHHC_CouchLED2_2;
				rvHHC_DisplayState_2      <= rvHHC_DisplayState_2;
				rvHHC_CouchLNGPLAN_2      <= rvHHC_CouchLNGPLAN_2;
				rvHHC_MLCX_2              <= rvHHC_MLCX_2;	
				rvHHC_MLCY_2              <= rvHHC_MLCY_2;
				end		
			endcase
			end
		else begin		
				rvBSC_LED_1               <=rvBSC_LED_1;		
				rvBSC_LED_2               <=rvBSC_LED_2;	
				rvHHC_Gantry_1            <= rvHHC_Gantry_1;
				rvHHC_Collimator_1        <= rvHHC_Collimator_1;		
				rvHHC_Swing_1             <= rvHHC_Swing_1;		
				rvHHC_MLCX1_1             <= rvHHC_MLCX1_1;		
				rvHHC_MLCX2_1             <= rvHHC_MLCX2_1;		
				rvHHC_JawY1_1             <= rvHHC_JawY1_1;		
				rvHHC_JawY2_1             <= rvHHC_JawY2_1;	
				rvHHC_CouchVRT_1          <= rvHHC_CouchVRT_1;
				rvHHC_CouchLAT_1          <= rvHHC_CouchLAT_1;	
				rvHHC_CouchLNG_1          <= rvHHC_CouchLNG_1;
				rvHHC_CouchLED1_1         <= rvHHC_CouchLED1_1;
				rvHHC_CouchLED2_1         <= rvHHC_CouchLED2_1;
				rvHHC_DisplayState_1      <= rvHHC_DisplayState_1;
				rvHHC_CouchLNGPLAN_1      <= rvHHC_CouchLNGPLAN_1;
				rvHHC_MLCX_1              <= rvHHC_MLCX_1;	
				rvHHC_MLCY_1              <= rvHHC_MLCY_1;	
				rvHHC_Gantry_2            <= rvHHC_Gantry_2;
				rvHHC_Collimator_2        <= rvHHC_Collimator_2;		
				rvHHC_Swing_2             <= rvHHC_Swing_2;		
				rvHHC_MLCX1_2             <= rvHHC_MLCX1_2;		
				rvHHC_MLCX2_2             <= rvHHC_MLCX2_2;		
				rvHHC_JawY1_2             <= rvHHC_JawY1_2;		
				rvHHC_JawY2_2             <= rvHHC_JawY2_2;	
				rvHHC_CouchVRT_2          <= rvHHC_CouchVRT_2;
				rvHHC_CouchLAT_2          <= rvHHC_CouchLAT_2;	
				rvHHC_CouchLNG_2          <= rvHHC_CouchLNG_2;
				rvHHC_CouchLED1_2         <= rvHHC_CouchLED1_2;
				rvHHC_CouchLED2_2         <= rvHHC_CouchLED2_2;
				rvHHC_DisplayState_2      <= rvHHC_DisplayState_2;
				rvHHC_CouchLNGPLAN_2      <= rvHHC_CouchLNGPLAN_2;
				rvHHC_MLCX_2              <= rvHHC_MLCX_2;	
				rvHHC_MLCY_2              <= rvHHC_MLCY_2;
			end
		end
	end

//generate init_hold signal
//ESC(lan9252) completely initialized after reset will take 7 seconds around
//the PRAM data will not be used before 10 seconds（init_hold==1)
always@(posedge iClk or negedge iRst_n)begin
	if(iRst_n==1'b0)begin
		rHold <= 1'b1;	
		rvCnt <= 32'd0;
		end
	else begin
		if(rvCnt < DLY_TIME)begin
			rvCnt <= rvCnt+1'b1;
			rHold <= 1'b1;
			end
		else begin
			rvCnt <= rvCnt;
			rHold <= 1'b0;	
			end
		end
	end		
endmodule


